using System.Web.UI;
using System.Collections.Generic;
using System.Web;
using System.Security.Permissions;
using System;

namespace SharpPieces.Web.Controls
{

    /// <summary>
    /// A collection of tag fields.
    /// </summary>
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class TagCloudFieldCollection : List<TagCloudField>, IStateManager
    {

        // Fields

        private bool tracks;

        // Events

        /// <summary>
        /// Occurs when the fields have changed.
        /// </summary>
        public event EventHandler FieldsChanged;

        // Methods

        /// <summary>
        /// When implemented by a class, loads the server control's previously saved view state to the control.
        /// </summary>
        /// <param name="state">An <see cref="T:System.Object"/> that contains the saved view state values for the control.</param>
        internal void LoadViewState(object state)
        {
            if (null != state)
            {
                object[] s = (object[])state;

                object[] data = s[0] as object[];

                if (null != data)
                {
                    for (int i = 0; i < this.Count; i++)
                    {
                        this[i].LoadViewState(data[i]);
                    }
                }
            }
        }

        /// <summary>
        /// When implemented by a class, saves the changes to a server control's view state to an <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// The <see cref="T:System.Object"/> that contains the view state changes.
        /// </returns>
        internal object SaveViewState()
        {
            object[] state = new object[1];

            object[] data = new object[this.Count];

            for (int i = 0; i < this.Count; i++)
            {
                data[i] = this[i].SaveViewState();
            }

            state[0] = data;

            return state;
        }

        /// <summary>
        /// When implemented by a class, instructs the server control to track changes to its view state.
        /// </summary>
        internal void TrackViewState()
        {
            this.tracks = true;

            foreach (TagCloudField field in this)
            {
                field.TrackViewState();
            }
        }

        // Properties

        /// <summary>
        /// When implemented by a class, gets a value indicating whether a server control is tracking its view state changes.
        /// </summary>
        /// <value></value>
        /// <returns>true if a server control is tracking its view state changes; otherwise, false.
        /// </returns>
        public bool IsTrackingViewState
        {
            get { return this.tracks; }
        }

        #region IStateManager Members

        /// <summary>
        /// When implemented by a class, gets a value indicating whether a server control is tracking its view state changes.
        /// </summary>
        /// <value></value>
        /// <returns>true if a server control is tracking its view state changes; otherwise, false.
        /// </returns>
        bool IStateManager.IsTrackingViewState
        {
            get { return this.IsTrackingViewState; }
        }

        /// <summary>
        /// When implemented by a class, loads the server control's previously saved view state to the control.
        /// </summary>
        /// <param name="state">An <see cref="T:System.Object"/> that contains the saved view state values for the control.</param>
        void IStateManager.LoadViewState(object state)
        {
            this.LoadViewState(state);
        }

        /// <summary>
        /// When implemented by a class, saves the changes to a server control's view state to an <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// The <see cref="T:System.Object"/> that contains the view state changes.
        /// </returns>
        object IStateManager.SaveViewState()
        {
            return this.SaveViewState();
        }

        /// <summary>
        /// When implemented by a class, instructs the server control to track changes to its view state.
        /// </summary>
        void IStateManager.TrackViewState()
        {
            this.TrackViewState();
        }

        #endregion

    }

}
